/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package mathma.core.bool;

import mathma.core.expr.Expression;

import mathma.core.form.MultiExpression;

import mathma.core.real.FiniteReal;
import mathma.core.real.Real;

import java.util.List;

/**
 * Counts true operands.  This returns the number of its operands that evaluate
 * to true.
 *
 * @since 0.2.4
 * @author Ethan Levine
 */
public class TrueCountExpression
        extends MultiExpression<Real, BooleanValue> {

    /**
     * Creates a new TrueCountExpression from a variable number of operands.
     *
     * @param operands the operands of this expression.
     */
    public TrueCountExpression(Expression<BooleanValue>... operands) {
        super(Real.Type, operands);
    }

    /**
     * Creates a new TrueCountExpression from a list of operands.
     *
     * @param operands the operands of this expression.
     */
    public TrueCountExpression(List<Expression<BooleanValue>> operands) {
        super(Real.Type, operands);
    }

    /**
     * Evaluates this expression.  This returns the count of how many operands
     * evaluate to true.
     *
     * @return how many operands evaluate to true.
     */
    public Real evaluate() {
        int value = 0;
        for (Expression<BooleanValue> operand : getOperands()) {
            if (operand.evaluate().getRawValue()) {
                value ++;
            }
        }
        return new FiniteReal(value);
    }

    public TrueCountExpression newInstance(List<Expression<BooleanValue>> operands) {
        return new TrueCountExpression(operands);
    }
}
